package com.ruibang.glass.iso.service.impl.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.Constants;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.iso.domain.req.PermissionManageReq;
import com.ruibang.glass.iso.domain.req.PermissionReq;
import com.ruibang.glass.iso.entity.PermissionManage;
import com.ruibang.glass.iso.mapper.PermissionManageMapper;
import com.ruibang.glass.iso.service.impl.PermissionManageService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-06-18  09:39
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class PermissionManageServiceImpl extends ServiceImpl<PermissionManageMapper, PermissionManage> implements PermissionManageService {

    @Autowired
    private RedisSingleService redisService;

    @Override
    public IPage<PermissionManage> pageQuery(PermissionManageReq permissionManageReq) {
        IPage<PermissionManage> page = baseMapper.pageQuery(new Page<>(permissionManageReq.getPageNo(), permissionManageReq.getPageSize()), permissionManageReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<PermissionManage> permissionManage = page.getRecords();
            for (PermissionManage manage : permissionManage) {
                if (Constants.ONE_STR.equals(manage.getObjType())) {
                    //部门
                    Object deptNameObj = redisService.hget(RedisKey.DEPT_GROUP_TYPE, manage.getObjId());
                    if (deptNameObj != null && StringUtils.isNotBlank(deptNameObj.toString())) {
                        manage.setObjName(deptNameObj.toString());
                    }
                } else if (Constants.TWO_STR.equals(manage.getObjType())) {
                    //用户
                    Object userNameObj = redisService.hget(RedisKey.USER_GROUP_TYPE, manage.getObjId());
                    if (userNameObj != null && StringUtils.isNotBlank(userNameObj.toString())) {
                        manage.setObjName(userNameObj.toString());
                    }
                }
            }
        }
        return page;
    }

    @Override
    public List<PermissionManage> getByObjId(PermissionReq permissionReq) {
        return baseMapper.getByObjId(permissionReq);
    }

    @Override
    public void editStatusById(List<String> ids, String status) {
        lambdaUpdate().set(PermissionManage::getStatus,status).in(PermissionManage::getId,ids).update();
    }

}
